// DialogSourceNodesSetup.cpp : implementation file
//

#include "stdafx.h"
#include "testSVG.h"
#include "testSVGDlg.h"
#include "DialogSourceNodesSetup.h"
#include ".\dialogsourcenodessetup.h"

extern MOLECULE* MoleculesList;


// CDialogSourceNodesSetup dialog

IMPLEMENT_DYNAMIC(CDialogSourceNodesSetup, CDialog)
CDialogSourceNodesSetup::CDialogSourceNodesSetup(CWnd* pParent /*=NULL*/)
	: CDialog(CDialogSourceNodesSetup::IDD, pParent)
{
}

CDialogSourceNodesSetup::~CDialogSourceNodesSetup()
{
}

void CDialogSourceNodesSetup::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CDialogSourceNodesSetup, CDialog)
	ON_BN_CLICKED(IDOK, OnBnClickedOk)
	ON_CBN_SELCHANGE(IDC_COMBO_COND_TYPE, OnCbnSelchangeComboCondType)
	ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel)
END_MESSAGE_MAP()


// CDialogSourceNodesSetup message handlers

void CDialogSourceNodesSetup::OnBnClickedOk()
{
	CString steps;

	CComboBox* myCombo = (CComboBox*) GetDlgItem(IDC_COMBO_STEPS);
	myCombo->GetWindowText(steps);

	m_steps = atoi(steps.GetBuffer());

	myCombo = (CComboBox*) GetDlgItem(IDC_COMBO_COND_TYPE);
	myCombo->GetWindowText(m_type);

	myCombo = (CComboBox*) GetDlgItem(IDC_COMBO_COND_NAME);
	myCombo->GetWindowText(m_name);

	myCombo = (CComboBox*) GetDlgItem(IDC_COMBO_DIRECTION);
	myCombo->GetWindowText(m_direction);

	OnOK();
}

BOOL CDialogSourceNodesSetup::OnInitDialog()
{
	CDialog::OnInitDialog();

	MOLECULE* tempNode;
	MOLECULE* tempNode1;
	int flag;
	int i;
	char dummy[10];

	CComboBox* comboType = (CComboBox*) GetDlgItem(IDC_COMBO_COND_TYPE);
	CComboBox* comboName = (CComboBox*) GetDlgItem(IDC_COMBO_COND_NAME);
	CComboBox* comboSteps = (CComboBox*) GetDlgItem(IDC_COMBO_STEPS);
	CComboBox* comboDirection = (CComboBox*) GetDlgItem(IDC_COMBO_DIRECTION);

	comboType->AddString("Type");
	comboType->AddString("Location");
	comboType->AddString("Name");

	comboDirection->AddString("Forward");
	comboDirection->AddString("Reverse");

	comboDirection->SetCurSel(0);
	comboType->SetCurSel(2);

	tempNode = MoleculesList;

	while (tempNode)
	{	
		tempNode1 = MoleculesList;
		flag = 0;
		while (tempNode1 != tempNode)
		{
			if (!strcmp(tempNode1->moleculeType, tempNode->moleculeType))
			{
				flag = 1;
				break;
			}
			tempNode1 = tempNode1->next;
		}
		if (!flag)
		{
			comboName->AddString(tempNode->moleculeType);
		}
		tempNode = tempNode->next;
	}
	
	comboName->SetCurSel(0);

	for (i = 3; i <= 20; i++)
	{
		comboSteps->AddString(itoa(i, dummy, 10));
	}
	
	comboSteps->SetCurSel(9);

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE
}

void CDialogSourceNodesSetup::OnCbnSelchangeComboCondType()
{
	CComboBox* myCombo;
	int nIndex;
	myCombo = (CComboBox*) GetDlgItem(IDC_COMBO_COND_TYPE);

	nIndex = myCombo->GetCurSel();

	switch (nIndex)
	{
	case 1:
		LoadNames();
		break;
	case 2:
		LoadTypes();
		break;
	case 0:
		LoadLocations();
		break;
	default:
		LoadTypes();
		break;
	}

	myCombo = (CComboBox*) GetDlgItem(IDC_COMBO_COND_NAME);

	myCombo->SetCurSel(0);
}

void CDialogSourceNodesSetup::LoadNames()
{
	MOLECULE* tempNode;
	tempNode = MoleculesList;
	CComboBox* myCombo;
	int i;

	myCombo = (CComboBox*) GetDlgItem(IDC_COMBO_COND_NAME);

	int size = myCombo->GetCount();

	for (i = 0; i < size; i++)
	{
		myCombo->DeleteString(0);
	}

	myCombo->SetEditSel(0, -1);
	myCombo->Clear();

	while (tempNode)
	{	
		myCombo->AddString(tempNode->name);
		tempNode = tempNode->next;
	}
}

void CDialogSourceNodesSetup::LoadTypes()
{
	MOLECULE* tempNode;
	MOLECULE* tempNode1;
	tempNode = MoleculesList;
	CComboBox* myCombo;
	int flag;
	int i;

	myCombo = (CComboBox*) GetDlgItem(IDC_COMBO_COND_NAME);

	int size = myCombo->GetCount();

	for (i = 0; i < size; i++)
	{
		myCombo->DeleteString(0);
	}

	myCombo->SetEditSel(0, -1);
	myCombo->Clear();

	while (tempNode)
	{	
		tempNode1 = MoleculesList;
		flag = 0;
		while (tempNode1 != tempNode)
		{
			if (!strcmp(tempNode1->moleculeType, tempNode->moleculeType))
			{
				flag = 1;
				break;
			}
			tempNode1 = tempNode1->next;
		}
		if (!flag)
		{
			myCombo->AddString(tempNode->moleculeType);
		}
		tempNode = tempNode->next;
	}
}

void CDialogSourceNodesSetup::LoadLocations()
{
	MOLECULE* tempNode;
	MOLECULE* tempNode1;
	tempNode = MoleculesList;
	CComboBox* myCombo;
	int flag;
	int i;

	myCombo = (CComboBox*) GetDlgItem(IDC_COMBO_COND_NAME);

	int size = myCombo->GetCount();

	for (i = 0; i < size; i++)
	{
		myCombo->DeleteString(0);
	}

	myCombo->SetEditSel(0, -1);
	myCombo->Clear();

	while (tempNode)
	{	
		tempNode1 = MoleculesList;
		flag = 0;
		while (tempNode1 != tempNode)
		{
			if (!strcmp(tempNode1->location, tempNode->location))
			{
				flag = 1;
				break;
			}
			tempNode1 = tempNode1->next;
		}
		if (!flag)
		{
			myCombo->AddString(tempNode->location);
		}
		tempNode = tempNode->next;
	}
}
void CDialogSourceNodesSetup::OnBnClickedCancel()
{
	// TODO: Add your control notification handler code here
	OnCancel();
}
